{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a21a5e53-4461-4839-ac82-8f64f8ef1816",
   "metadata": {},
   "source": [
    "# 第一讲 进制\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fd47fec7-7805-42ed-b342-f7fc3ec10cd0",
   "metadata": {},
   "source": [
    "## 进制转换\n",
    "\n",
    "\n",
    "### 其他进制-转10进制\n",
    "\n",
    "1. 短除法\n",
    "   > + 除(转的进制数)取余,取余数\n",
    "   > + 逆序输出余数\n",
    "\n",
    "2. 手指法(二进制)\n",
    "\n",
    "   > 按位数拼接\n",
    "   > 97 64+32+1  对应每个位数标注\n",
    "   >\n",
    "### 10进制转其他进制\n",
    "\n",
    "> 当前数字x进制**位数   的和\n",
    "   \n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2a3c7d99-eabf-46e5-b9fc-a276281ae410",
   "metadata": {},
   "source": [
    "## python的进制转换(二进制和十进制)\n",
    "\n",
    "1. bin(十进制) 转为2进制输出(字符串类型)\n",
    "2. 数字表示二进制 必须以0b开头 `0b10101`,输出十进制\n",
    "3. int('10101',2)强制类型转换,二进制字符串,转为int值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "8e98ac22-08d2-4266-bab7-207978be0562",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0b10101\n",
      "21\n",
      "21\n"
     ]
    }
   ],
   "source": [
    "print(bin(21))\n",
    "\n",
    "a = 0b10101\n",
    "print(a)\n",
    "\n",
    "#强制类型转换\n",
    "bin_str = '10101'\n",
    "print(int(bin_str,2))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8e9b3db5-2782-43de-8414-99d7a3433534",
   "metadata": {},
   "source": [
    "## python的进制转换(八进制和十进制)\n",
    "\n",
    "1. oct(十进制) 转为8进制输出(字符串类型)\n",
    "2. 数字表示八进制 必须以0o开头 `0o1276`,输出十进制\n",
    "3. int('31',8)强制类型转换,八进制字符串,转为int值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "1e61db2f-e08f-4080-ab2b-4ed719a9a42e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0o12\n",
      "0xa\n",
      "0b1010\n",
      "84\n",
      "2748\n",
      "42\n",
      "10\n",
      "520\n",
      "2748\n"
     ]
    }
   ],
   "source": [
    "#10转8进制\n",
    "print(oct(10))\n",
    "#10转16\n",
    "print(hex(10))\n",
    "#10转2\n",
    "print(bin(10))\n",
    "\n",
    "#表示8进制\n",
    "print(0o124)\n",
    "#表示16进制\n",
    "print(0xabc)\n",
    "#表示2进制\n",
    "print(0b101010)\n",
    "\n",
    "#类型转换\n",
    "print(int('1010',2))#2转10\n",
    "print(int('1010',8))#8转10\n",
    "print(int('abc',16))#16转10"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "632f6d41-c419-4737-b19e-316418efe393",
   "metadata": {},
   "source": [
    "## python的进制转换(十六进制和十进制)\n",
    "\n",
    "1. hex(十进制) 转为16进制输出(字符串类型)\n",
    "2. 数字表示八进制 必须以0x开头 `0x1276`,输出十进制\n",
    "3. int('31',16)强制类型转换,十六进制字符串,转为int值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "3d38c111-f4b1-4ada-92dc-7fdf8d568534",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (3581381350.py, line 3)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  Cell \u001b[1;32mIn[22], line 3\u001b[1;36m\u001b[0m\n\u001b[1;33m    a ---> 1010\u001b[0m\n\u001b[1;37m        ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "#16进制数 ----> 2进制数*4\n",
    "\n",
    "a ---> 1010\n",
    "\n",
    "d2945\n",
    "11010010100101000101"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "35404014-3711-4b94-9b4a-ef0f5cb6aa92",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'0b1001'"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bin(0b1001)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "6715a99a-ed75-4037-a2c9-50d3265f1825",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 2}\n",
      "{1, 2, 3, 4, 5}\n",
      "{3, 4}\n",
      "{1, 2, 5}\n"
     ]
    }
   ],
   "source": [
    "set1 = {1,2,3,4,4}\n",
    "set2 = {3,4,5}\n",
    "\n",
    "#差集(减去)\n",
    "print(set1-set2)\n",
    "\n",
    "#合集(合在一起)\n",
    "print(set1|set2)\n",
    "\n",
    "#交集(只要重复的)\n",
    "print(set1&set2)\n",
    "\n",
    "#对称差集(只要不重复)\n",
    "print(set1^set2)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
